package com.example.test_load_pic_rough.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.test_load_pic_rough.R;
import com.example.test_load_pic_rough.bean.User;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;

import java.util.ArrayList;

public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.UserListItemHolder> {

    ArrayList<User> mList ;

    public UserListAdapter(ArrayList<User> mList) {
        this.mList = mList;
    }

    public ArrayList<User> getmList() {
        return mList;
    }

    @NonNull
    @Override
    public UserListItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_list_item, parent, false);
        return new UserListItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull UserListItemHolder holder, int position) {
        User user = mList.get(position);
        holder.textView.setText(user.getName());
        holder.textView.setOnClickListener(view -> Toast.makeText(holder.textView.getContext() , user.getName(),Toast.LENGTH_SHORT).show());
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    class UserListItemHolder extends RecyclerViewHolder {

        private TextView textView ;

        public UserListItemHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.user_name);
        }
    }
}
